home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1597 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: my atoi function, could someone suggest...
  5. Date: Mon, 15 Jan 96 15:30:01 GMT
  6. Organization: none
  7. Message-ID: <821719801snz@genesis.demon.co.uk>
  8. References: <4cf7ap$q4u@kaleka.seanet.com> <4cq937$if9@ns.RezoNet.NET> <4d6v51$qoh@gryphon.phoenix.net> <821573157snz@genesis.demon.co.uk> <4dbjsf$gmt@ns.RezoNet.NET>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4dbjsf$gmt@ns.RezoNet.NET> ray@ultimate-tech.com "Ray Dunn" writes:
  15.  
  16. >In referenced article, Lawrence Kirby says...
  17. >>        unsigned value = 0;
  18. >>        unsigned digit = *str - '0';
  19. >>
  20. >>        if (digit < 10) {
  21. >>            value = digit;
  22. >
  23. >This is just a matter of personal style, but using the fact that an 
  24. >unsigned number minus a larger number results in a large unsigned 
  25. >number (so that "digit < 10" is false when *str is less than '0'), is 
  26. >rather obtuse IMO.
  27.  
  28. It is sometimes interesting to present alternatives and discuss their
  29. merits afterwards. I did stress that the code was simply intended to show
  30. alternative approaches. As for being 'obtuse' I'd say that the only
  31. reason that, say,
  32.  
  33.  if ((ch = getchar()) != EOF)
  34.  
  35. isn't obtuse is because it is a commonly encountered idiom. If people are
  36. familiar with the unsigned test above it ceases to be obtuse (and to, say,
  37. some programmers with machine code experience it may seem completely
  38. natural). Whether it is sensible code to thrust onto the great unwashed masses
  39. of C programmers is certainly a debatable point! :-)) I think C programmers
  40. should have a good feel for unsigned integer types and examples like this
  41. help with that. As an aside a portable macro implementation of isdigit()
  42. which may be the most efficient on some systems is:
  43.  
  44. #define isdigit(ch) ((unsigned)+(ch) - '0' < 10)
  45.  
  46. In this approach ch is only evaluated once so the macr doesn't suffer
  47. fromt side-effect problems. The unary + allows the compiler to diagnose
  48. illegal arguments such as pointers which would be accepted by the cast
  49. alone.
  50.  
  51. -- 
  52. -----------------------------------------
  53. Lawrence Kirby | fred@genesis.demon.co.uk
  54. Wilts, England | 70734.126@compuserve.com
  55. -----------------------------------------
  56.